
import worker from '@ohos.worker'
import filemanager from '@ohos.fileManager'

var TAG = 'FileDirWorker'
const parentPort = worker.parentPort
parentPort.onmessage = function (e) {
    let data = e.data
    if (data.request_data == 'getRoot') {
        getRootData(data)
    } else if (data.request_data == 'listFile') {
        getListFileData(data)
    } else if (data.request_data == 'createFile') {
        createFile(data);
    }


}

parentPort.onmessageerror = function () {
    console.log('aaa-->onmessageerror')
}

parentPort.onerror = function (data) {
    console.log('aaa-->onerror')
}

function getRootData(data) {
    filemanager.getRoot()
        .then(file => {
            handleData(file, data)

        })
        .catch((error) => {
            console.log('aaa--getRoot 2 error' + error)
        });
}

function getListFileData(data)
{
    if (data.media_type == 'file')
    {
        console.log('file data.path='+data.path)
        filemanager.listFile(data.path, data.media_type)
            .then(fileFolder => {
                if(Array.isArray(fileFolder))
                {
                    for (var i = 0; i < fileFolder.length; i++)
                    {
                        var folder = fileFolder[i]
                        console.log('file folder.path='+folder.path)
                        filemanager.listFile(folder.path, 'file')
                            .then(fileList => {
                                handleData(fileList, data)
                            })
                            .catch((error) => {
                                console.log('aaa--folderError' + error)
                                handleData([], data)

                            })
                    }
                }
            })
            .catch((error) =>
            {
                console.log('aaa--getFolderDataError' + error)
                handleData([], data)

            })
    }
    else
    {
        console.log('!file data.path='+data.path+',data.media_type='+data.media_type)
        filemanager.listFile(data.path, 'file')
            .then(fileFolder => {
                if(Array.isArray(fileFolder))
                {
                    for (var i = 0; i < fileFolder.length; i++)
                    {
                        var folder = fileFolder[i]
                        console.log('!file folder.path='+folder.path+',data.media_type='+data.media_type)
                        filemanager.listFile(folder.path, 'file')
                            .then(fileList => {
                                if(Array.isArray(fileList)){
                                    for (var index = 0; index < fileList.length; index++)
                                    {
                                        var file = fileList[index]
                                        console.log( '!file data.path='+data.path+',file.path='+file.path+',data.media_type='+data.media_type)
                                        filemanager.listFile(data.path+file.path, data.media_type)
                                            .then(fileItem => {
                                                handleData(fileItem, data)
                                            })
                                            .catch((error) => {
                                                console.log('aaa--fileListError' + error)
                                                handleData([], data)

                                            })
                                    }
                                }
                            })
                            .catch((error) => {
                                console.log('aaa--fileError' + error)
                                handleData([], data)

                            })
                    }
                }
            })
            .catch((error) =>
            {
                console.log('aaa--getFileError' + error)
            })
    }
}




//function getListFileData(data) {
//    LogDebug(TAG, 'getListFileData data.offset='+data.offset )
//    LogDebug(TAG, 'getListFileData data.count='+data.count )
//    if (data.offset == undefined || data.count == undefined) {
//        LogDebug(TAG, 'path = ' + data.path + " type = " + data.media_type)
//
//        //        let options = {
//        //            "dev": {
//        //                "name": "external_storage"
//        //            }
//        //        }
//        filemanager.listFile(data.path, data.media_type)
//            .then(file => {
//                handleData(file, data)
//            })
//            .catch((error) => {
//                LogDebug(TAG, 'getListFileData 2 error' + error)
//            })
//
//    }
//    else
//    {
//        filemanager.listFile(data.path, data.media_type, {
//            'offset': data.offset,
//            'count': data.count
//        })
//            .then(file => {
//                handleData(file, data)
//            })
//            .catch((error) => {
//                LogDebug(TAG, 'getListFileData 3 error' + error)
//            })
//    }
//}




function createFile(data) {
    filemanager.createFile(data.path, data.save_name)
        .then((uri) => {
            handleData(uri, data)
        })
        .catch((err) => {
            handleData(err, data)
        })
}



//function getDir(data) {
//    let dir = fileio.opendirSync(path);
//    dir.read().then(function (dirent){
//        console.log("read successfully:"+JSON.stringify(dirent));
//    }).catch(function(err){
//        console.info("read failed with error:"+ err);
//    });
//}

function handleData(file, data) {
    console.log( 'handleData')
    var info = JSON.stringify(file)
    console.log( 'info leng= ' + info.length)
    console.log( 'info text = ' + info)
    var buf = new ArrayBuffer(info.length * 2)
    var bufView = new Uint16Array(buf)
    for (var index = 0; index < info.length; index++)
    {
        bufView[index] = info.charCodeAt(index)
    }
    console.log( 'bufView text = ' + bufView)
    console.log( 'data text = ' + data)
    parentPort.postMessage({ data: bufView, params: data })
}